/**
 * 版权所有 (TMS)
 */
package com.xiweicheng.test.jpa.postgres;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Properties;

/**
 * @author xiweicheng
 * @date 2021/8/28 7:08 下午
 */
@Slf4j
public class SnowFlakeGenerator implements IdentifierGenerator, Configurable {

    private SnowFlake snowFlake;

    @Override
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
        this.snowFlake = new SnowFlake(1L, 1L);
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        long id = this.snowFlake.nextId();
        log.info("snowflake id:{}", id);
        return id;
    }
}
